package com.hnevc.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.hnevc.usbdemo.Mouse;



public class MouseEventDemo extends JFrame{
	int pre_x=-1, pre_y=-1;
	int x,y;//鼠标当前位置
	
	class PaintListener implements MouseMotionListener{

		@Override
		public void mouseDragged(MouseEvent e) {
			x = e.getX();
			y = e.getY();
			MouseEventDemo.this.paint(panel.getGraphics());
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			
		}
		
	}
	class ResetListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			if(e.getButton() == MouseEvent.BUTTON3){
				panel.repaint();
				//MouseEventDemo.this.repaint();
			}
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			pre_x = -1;
			pre_y = -1;
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	JPanel panel;
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		//super.paint(g);
		g.setColor(Color.red);
		if(pre_x >0  && pre_y >0 ){
			g.drawLine(pre_x, pre_y, x, y);
		}
		pre_x = x;
		pre_y = y;
	}
	public MouseEventDemo(){
		super("画板");
		panel = new JPanel();
		panel.addMouseMotionListener(new PaintListener());
		panel.addMouseListener(new ResetListener());
		
		this.add(panel);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(200, 200);
		this.setSize(200, 300);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MouseEventDemo();
	}

}
